<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>10、找到最接近0的数字</title>
</head>

<body>
  <script>
    var findClosestNumber = function (nums) {
      nums.sort((a, b) => a - b);
      let ans = 0, min = Infinity;
      for (const num of nums) {
        let temp = Math.abs(num);
        if (temp <= min) {
          min = temp;
          ans = num;
        }
      }
      return ans;
    };
    console.log(findClosestNumber([-4, -2, 1, 4, 8]))
  </script>
</body>

</html>